using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

using TrueCode.Core;

namespace EMan.GUI
{
  public class CollectionThreadSafeWrapper : DependencyObject, IWeakEventListener//, IEnumerable
  {
    public static readonly DependencyProperty SourceProperty = DependencyProperty
      .Register( "Source", typeof( object ), typeof( CollectionThreadSafeWrapper ), 
        new FrameworkPropertyMetadata( null, On_SourceChanged ), On_ValidateValueCallback );

    public static readonly DependencyProperty WidthProperty = DependencyProperty
      .Register( "Width", typeof( bool ), typeof( CollectionThreadSafeWrapper ), 
        new FrameworkPropertyMetadata( false, On_SourceChanged, On_SourceCoerceValue ) );


    private IEnumerable _items;

    public object Source
    {
      get{ return GetValue( SourceProperty); }
      set { SetValue( SourceProperty, value ); }
    }

    public bool Width
    {
      get{ return (bool)GetValue( WidthProperty ); }
      set{ SetValue( WidthProperty, value );}
    }

    #region Overrides of DependencyObject
    protected override void OnPropertyChanged( DependencyPropertyChangedEventArgs e )
    {
      
    }

    protected override bool ShouldSerializeProperty( DependencyProperty dp )
    {
      return true;
    }
    #endregion

    private static void On_SourceChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
      var xz = e.NewValue;
    }

    private static object On_SourceCoerceValue( DependencyObject d, object baseValue )
    {
      return baseValue;
    }

     private static bool On_ValidateValueCallback(object value)
     {
//          if (((value != null) && !(value is IEnumerable)) && (!(value is IListSource) && !(value is DataSourceProvider)))
//    {
//        return false;
//    }
//       return !( value is CollectionThreadSafeWrapper );
       return true;

     }


    #region Implementation of IEnumerable
    public IEnumerator GetEnumerator()
    {
      return _items == null ? EmptyArray< bool >.Instance.GetEnumerator() : _items.GetEnumerator();
    }
    #endregion

    #region Implementation of IWeakEventListener
    public bool ReceiveWeakEvent( Type managerType, object sender, EventArgs e )
    {
      return true;
    }
    #endregion
  }
}